package com.zhongke.cotrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.RegionMapper;
import com.zhongke.cotrun.mapper.ServiceReleaseMapper;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.*;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.ServiceReleaseService;
import com.zhongke.cotrun.service.ServiceTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ServiceReleaseImpl implements ServiceReleaseService {
    @Autowired
    ServiceReleaseMapper serviceReleaseMapper;
    @Autowired
    ServiceTypeService serviceTypeService;
    @Autowired
    RegionMapper regionMapper;

    /**
     * 服务发布
     *
     * @param serviceRelease
     * @param session
     * @return
     */
    @Override
    public Boolean addServiceRelease(ServiceRelease serviceRelease, HttpSession session) {

        return serviceReleaseMapper.insertServiceRelease(serviceRelease);
    }

    /**
     * 查询服务
     *
     * @param serviceRelease
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo selectServiceReleaseBycondition(ServiceRelease serviceRelease, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ServiceReleaseVo> s = serviceReleaseMapper.selectSercicereleaseByCondition(serviceRelease);

        if (s != null && s.size() != 0) {
            for (ServiceReleaseVo sr : s) {
                UserInforCompany uf = sr.getUserInforCompany();
                if (uf != null) {
                    String city = uf.getCity();
                    if (StringUtils.isNotEmpty(city)) {
                        Region region = regionMapper.selectByPrimaryKey(Integer.valueOf(city));
                        if (region != null) {
                            uf.setCityName(region.getName());
                        }
                    }
                }
                Integer code = sr.getSerciceType();

                isServiceType(code, sr);

            }
        }


        PageInfo pageInfo = new PageInfo(s);
        return pageInfo;
    }

    /**
     * 根据id查看具体服务信息
     *
     * @param id
     * @return
     */
    @Override
    public ServiceReleaseVo selectReleaseById(Integer id) throws MyException {


        ServiceReleaseVo sv = serviceReleaseMapper.selectServiceReleaseById(id);
        if (sv == null) {
            throw new MyException(CodeMsg.NOINFORMATION_ERROP);
        }
//        BeanUtils.copyProperties(s,sv);//拷贝属性
        Integer code = sv.getSerciceType();//获取服务的类型的值
        isServiceType(code, sv);

        return sv;
    }

    /**
     * 修改服务
     *
     * @param serviceRelease
     * @return
     */
    @Override
    public boolean updateServiceRelease(ServiceRelease serviceRelease) {


        return serviceReleaseMapper.updateSelectServiceRelease(serviceRelease);
    }

    /**
     * 校验服务类型转换
     */
    public ServiceReleaseVo isServiceType(Integer code, ServiceReleaseVo sv) {
        if (code != null) {

            ServiceTypeVO l = serviceTypeService.selectServiceTypeBycode(code);

            if (l != null) {
                sv.setSerciceTypeNme(l.getTypeName());

            }
        }
        return sv;
    }

    /**
     * 删除服务
     *
     * @param id
     * @param statue
     * @return
     */
    @Override
    public boolean delectSerciceRelease(Integer id, Integer statue) {

        return serviceReleaseMapper.delectServiceRelease(id, statue);
    }
}
